
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[CountryTournaments_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[CountryTournaments_Insert]

IF OBJECT_ID(N'[dbo].[CountryTournaments_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[CountryTournaments_Update]

IF OBJECT_ID(N'[dbo].[CountryTournaments_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[CountryTournaments_Delete]

IF OBJECT_ID(N'[dbo].[CountryTournaments_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[CountryTournaments_Select]

--endregion

GO


--region [dbo].[CountryTournaments_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[CountryTournaments_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdCountryTournament int = null,
	@Name nvarchar(100) = null,
	@Icon nvarchar(100) = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[CountryTournaments]
					WHERE
					(
						(@IdCountryTournament is null OR [CountryTournaments].[IdCountryTournament] = @IdCountryTournament)
						AND (@Name is null OR [CountryTournaments].[Name] like @Name)
						AND (@Icon is null OR [CountryTournaments].[Icon] like @Icon)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[CountryTournaments].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdCountryTournament' and @SortType = 1 THEN [CountryTournaments].[IdCountryTournament] END ASC,
						CASE WHEN @SortBy = 'IdCountryTournament' and @SortType = 0 THEN [CountryTournaments].[IdCountryTournament] END DESC,
						CASE WHEN @SortBy = 'Name' and @SortType = 1 THEN [CountryTournaments].[Name] END ASC,
						CASE WHEN @SortBy = 'Name' and @SortType = 0 THEN [CountryTournaments].[Name] END DESC,
						CASE WHEN @SortBy = 'Icon' and @SortType = 1 THEN [CountryTournaments].[Icon] END ASC,
						CASE WHEN @SortBy = 'Icon' and @SortType = 0 THEN [CountryTournaments].[Icon] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[CountryTournaments]
			Where 
			(
				(@IdCountryTournament is null OR [CountryTournaments].[IdCountryTournament] = @IdCountryTournament)
				AND (@Name is null OR [CountryTournaments].[Name] like @Name)
				AND (@Icon is null OR [CountryTournaments].[Icon] like @Icon)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[CountryTournaments_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[CountryTournaments_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[CountryTournaments_Insert]
	@IdCountryTournament int OUTPUT,
	@Name nvarchar(100),
	@Icon nvarchar(100)

AS


INSERT INTO [dbo].[CountryTournaments] 
(
	[Name],
	[Icon]
)
VALUES 
(
	@Name,
	@Icon
)

SET @IdCountryTournament = SCOPE_IDENTITY()

--end [dbo].[CountryTournaments_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[CountryTournaments_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[CountryTournaments_Update]
	@IdCountryTournament int,
	@Name nvarchar(100),
	@Icon nvarchar(100)
AS


UPDATE [dbo].[CountryTournaments] SET
	[Name] = @Name,
	[Icon] = @Icon
WHERE
	[IdCountryTournament] = @IdCountryTournament

--end [dbo].[CountryTournaments_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[CountryTournaments_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[CountryTournaments_Delete]
	@IdCountryTournament int
AS


DELETE FROM [dbo].[CountryTournaments]
WHERE
(
	[IdCountryTournament] = @IdCountryTournament
)

--end [dbo].[CountryTournaments_Delete]
--endregion

GO
--=========================================================================================--

